package com.wl.controller;

import com.wl.pojo.PageBean;
import com.wl.pojo.Personal.Collect;
import com.wl.pojo.Post;
import com.wl.service.PostService;
import com.wl.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Slf4j
@RestController
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 发布文章接口
     * 将文章数据写入数据库
     */
    @PostMapping("/publish/post")
    public Result PublishPost(@RequestBody Post post) throws IOException {
        postService.publishPost(post);
        return Result.success();
    }

    /**
     * 修改文章接口
     */
    @PostMapping("/publish/update")
    public Result UpdatePost(@RequestBody Post post) throws IOException {
        postService.updatePost(post);
        return Result.success();
    }

    /**
     * 获取标签数据
     */
    @GetMapping("/publish/tags")
    public Result getTags() {
        return Result.success(postService.getTags());
    }

    /**
     * 获取首页展示的文章数据
     */
    @GetMapping("/publish/post")
    public Result getPosts(Integer userId, Integer categoryId, Integer isDescending,
                           @RequestParam(defaultValue = "1") Integer PostCurrentPage,
                           @RequestParam(defaultValue = "10") Integer PostPageSize) {
        PageBean pageBean = postService.getPosts(userId, categoryId,isDescending,PostCurrentPage,PostPageSize);
        return Result.success(pageBean);
    }

    /**
     * 根据浏览量和点赞量进行降序排序查询热点数据
     */
    @GetMapping("/publish/hot")
    public Result getHotPosts() {
        return Result.success(postService.getHotPosts());
    }

    /**
     * 根据ID查询文章的详细信息
     */
    @GetMapping("/post/id")
    public Result getPostById(Integer postId) {
        return Result.success(postService.getPostById(postId));
    }

    /**
     * 每次点击一次文章，访问量加一
     */
    @PutMapping("/add/post/page/view")
    public Result addPostPageview(Integer postId) {
        postService.addPostPageview(postId);
        return Result.success();
    }

    /**
     * 根据文章id查询文章的作者信息
     */
    @GetMapping("/post/user")
    public Result getUserByPostId(Integer postId) {
        return Result.success(postService.getUserByPostId(postId));
    }

    /**
     * 查看某个用户是否关注了文章作者
     */
    @GetMapping("/is/follow")
    public Result getIsFollow(Integer userId, Integer followId) {
        return Result.success(postService.getIsFollow(userId, followId));
    }

    /**
     * 关注接口
     */
    @PostMapping("/active/follow")
    public Result activeFollow(Integer userId, Integer followId) {
        postService.activeFollow(userId, followId);
        return  Result.success();
    }

    /**
     * 取消关注接口
     */
    @PostMapping("/cancel/follow")
    public Result cancelFollow(Integer userId, Integer followId) {
        postService.cancelFollow(userId, followId);
        return  Result.success();
    }

    /**
     * 判断某个用户是否对某篇文章点赞
     */
    @GetMapping("/is/like")
    public Result isLike(Integer userId, Integer postId) {
        return Result.success(postService.getIsLike(userId, postId));
    }

    /**
     * 点赞请求
     */
    @PostMapping("/active/like")
    public Result activeLike(Integer userId, Integer postId) {
        postService.activeLike(userId, postId);
        return  Result.success();
    }

    /**
     * 取消点赞接口
     */
    @PostMapping("/cancel/like")
    public Result cancelLike(Integer userId, Integer postId) {
        postService.cancelLike(userId, postId);
        return  Result.success();
    }

    /**
     * 获取文章有多少点赞数量
     */
    @GetMapping("/like/count")
    public Result getLikeCount(Integer postId) {
        return Result.success(postService.getLikeCount(postId));
    }

    /**
     * 收藏请求
     */
    @PostMapping("/active/collect")
    public Result activeCollect(Integer userId, Integer postId, Integer categoryId) {
        postService.activeCollect(userId, postId, categoryId);
        return  Result.success();
    }

    /**
     * 取消收藏接口
     */
    @PostMapping("/cancel/collect")
    public Result cancelCollect(Integer userId, Integer postId, Integer categoryId) {
        postService.cancelCollect(userId, postId, categoryId);
        return  Result.success();
    }

    /**
     * 查看收藏数量
     */
    @GetMapping("/collect/count")
    public Result getCollectCount(Integer postId) {
        return Result.success(postService.getCollectCount(postId));
    }

    /**
     * 判断某个用户是否对某篇文章收藏
     */
    @GetMapping("/is/collect")
    public Result isCollect(Integer userId, Integer postId) {
        return Result.success(postService.getIsCollect(userId, postId));
    }

    /**
     * 创建收藏夹
     * @param collect collect 包含userId, postId, categoryId的实体类
     * @return Result 成功返回success，失败返回error
     */
    @PostMapping("/create/collect/category")
    public Result createCategory(@RequestBody Collect collect) {
        postService.createCategory(collect);
        return Result.success();
    }
}
